import mysql, { escape } from 'mysql'

export enum sqlErrorCode {
    exists = 1
}

const connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: 'Q<;Of<fk,25Y',
    database: 'warehouse'
})

const query = (sql: string) => {
    return new Promise((resolve, reject) => {
        connection.query(sql, (error, results, fields) => {
            if (error || (sql.trim().startsWith('INSERT INTO') && results.affectedRows !== 1)) {
                reject(error || sqlErrorCode.exists)
            } else {
                // @ts-expect-error 需要两个参数
                resolve(results, fields)
            }
        })
    })
}

export {
    connection,
    query,
    escape
}

// connection.connect()

// connection.end()
